<template>
	<section>

		<el-row>
			<el-col :span="12"><div class="grid-content bg-purple">

                <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
					<el-form :inline="true" :model="filters">
						<el-form-item>
							<el-button type="primary" @click="handleAdd">新增</el-button>
						</el-form-item>
					</el-form>
				</el-col>

				<!--列表1-->
				<el-table
                        :data="dictionarys"
                        highlight-current-row
                        v-loading="listLoading"
                        @current-change="selsChange"
                        style="width: 100%;">
					<el-table-column type="index" width="60">
					</el-table-column>
					<el-table-column prop="sn" label="产品类型英文名" width="150" sortable>
					</el-table-column>
					<el-table-column prop="name" label="产品类型" width="120" sortable>
					</el-table-column>


					<el-table-column label="操作" width="150">
						<template scope="scope">
							<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
							<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
						</template>
					</el-table-column>
				</el-table></div></el-col>

			<el-col :span="12"><div class="grid-content bg-purple-light">

				<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
					<el-form :inline="true" :model="filters">
						<el-form-item>
							<el-button type="primary" @click="DicAdd">新增</el-button>
						</el-form-item>
					</el-form>
				</el-col>
				<!--列表2-->
				<el-table :data="diction" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">

					<el-table-column type="index" width="60">
					</el-table-column>
					<el-table-column prop="name" label="产品名字" width="120" sortable>
					</el-table-column>

					<el-table-column label="操作" width="150">
						<template scope="scope">
							<el-button size="small" @click="handleDic(scope.$index, scope.row)">编辑</el-button>
							<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
						</template>
					</el-table-column>
				</el-table></div></el-col>
		</el-row>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
		<el-pagination layout="prev, pager, next"
					   @current-change="handleCurrentChange"
					   :page-size="pageSize"
					   :total="total"
					   style="float: right;">
		</el-pagination>
	</el-col>

		<!--编辑和新增都是同一界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="150px" :rules="editFormRules" ref="editForm">
				<el-form-item label="产品类型英文名" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="产品类型" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--编辑和新增都是同一界面-->
		<el-dialog :title="title" :visible.sync="dicFormVisible" :close-on-click-modal="false">
			<el-form :model="dicForm" label-width="150px" :rules="dicFormRules" ref="dicForm">
				<el-form-item label="产品名字" prop="name">
					<el-input v-model="dicForm.name" auto-complete="off"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="dicFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="DicSubmit" :loading="dicLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
    import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    keyname: ''
                },
                dictionarys: [],
                diction: [],
                total: 0,
                pageSize:5,
                currentPage: 1,
                listLoading: false,
                sels: [],//列表选中列
                title:'',
                editFormVisible: false,//编辑页面默认为false不显示，必须点击新增才打开u
                dicFormVisible: false,
                editLoading: false,
                dicLoading: false,
                editFormRules: {
                    name: [
                        { required: true, message: '请输入产品类型英文名', trigger: 'blur' }
                    ],
                    sn: [
                        { required: true, message: '请输入产品类型', trigger: 'blur' }
                    ]
                },
				dicFormRules:{
                    name: [
                        { required: true, message: '请输入品牌名字', trigger: 'blur' }
                    ]
				},
                //编辑界面数据
                editForm: {
                    id: null,
                    sn: "",
                    name:""
                },
                dicForm: {
                    id: null,
                    name:""
                },
                typeId:null
            }
        },
        methods: {
            handleCurrentChange(val) {
                this.currentPage = val;
                this.getDictionarys();
                this.getDictionary()
            },
            //获取左边部门列表
            getDictionarys() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize:this.pageSize,
                };
                this.listLoading = true; // 忙等框

                // 发送请求
                this.$http.post("/dictionary/getTypeList",para)
                    .then(result=>{
                        console.log(result)
                        this.dictionarys = result.data.rows;
                        this.total = result.data.totals;

                        this.listLoading = false; // 关闭忙等框
                    }).catch(result=>{
                    //console.debug(result)
                    alert("系统繁忙")
                })
				// this.getDictionary()
            },
            // 获取右边的列表
			getDictionary() {
                let para = {
                    currentPage: this.currentPage,
                    pageSize:this.pageSize,
                    // keyname: this.filters.keyname
                };
                this.listLoading = true; // 忙等框

                // 发送请求
                this.$http.post("/Dictionary/addRigitList",para)
                    .then(result=>{
                        // 打印输出验证是否有数据发送
                        // console.log(result)
                        this.diction = result.data.rows;
                        this.total = result.data.totals;

                        this.listLoading = false; // 关闭忙等框
                    }).catch(result=>{
                    console.debug(result)
                    alert("系统繁忙")
                })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.delete("/dictionary/"+row.id)
                        .then(result=>{
                            this.listLoading = false;
                            if (result.data.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.data.message,
                                    type: 'error'
                                });
                            }
                            this.currentPage = 1;
                            this.getDictionarys()
                        }).catch(result=>{
                        this.$message({
                            message: "系统繁忙",
                            type: 'error'
                        });
                    })
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.title = "编辑";
                this.editFormVisible = true;
                this.editForm = Object.assign({},row)
                // 回显数据
                if(this.editForm.dirPath){
                    let dirPath = this.editForm.dirPath
                    let arr = dirPath.split("/")
                    let parr = []
                    for(let i= 1;i<arr.length-1;i++){
                        // 强转但字符串只能是数字
                        parr.push(parseInt(arr[i]))
                    }
                    this.editForm.parent = parr
                }
            },
            handleDic: function (index, row) {
                this.title = "编辑";
                this.dicFormVisible = true;
                this.dicForm = Object.assign({},row)
                // 回显数据
                if(this.dicForm.dirPath){
                    let dirPath = this.dicForm.dirPath
                    let arr = dirPath.split("/")
                    let parr = []
                    for(let i= 1;i<arr.length-1;i++){
                        // 强转但字符串只能是数字
                        parr.push(parseInt(arr[i]))
                    }
                    this.dicForm.parent = parr
                }
            },
            //显示新增界面
            handleAdd: function () {
                this.title = "新增";
                this.editFormVisible = true;
                this.editForm = {
                    id: null,
                    sn: "",
                    name:"",
                };
            },
            DicAdd: function () {
                this.title = "新增";
                this.dicFormVisible = true;
                this.dicForm = {
                    id: null,
                    name:"",
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;// 忙等框
                            let para = Object.assign({}, this.editForm);
                            // 处理传输数据格式
                            if(para.parent){
                                let p = para.parent // 取出数组
                                let id = p[p.length-1]// 取数组中最后一个值
                                para.parent = {"id" : id}
                            }

                            this.$http.put("/dictionary",para)
                                .then(result=>{
                                    this.editLoading = false;//关闭忙等框
                                    if(result.data.success){
                                        this.$message({
                                            message: '提交成功',
                                            type: 'success'
                                        });
                                    }else{
                                        this.$message({
                                            message: result.data.message,
                                            type: 'error'
                                        });
                                    }
                                    this.editFormVisible = false;
                                    this.getDictionarys();
                                }).catch(result=>{
                                this.$message({
                                    message: "访问数人数过多请稍后再试",
                                    type: 'error'
                                });
                            })

                        });
                    }
                });
            },
            //右边新增编辑处理请求
            DicSubmit: function () {
                this.$refs.dicForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.dicLoading = true;// 忙等框
                            let para = Object.assign({}, this.dicForm);
                            // 处理传输数据格式
                            // if(para.parent){
                            //     let p = para.parent // 取出数组
                            //     let id = p[p.length-1]// 取数组中最后一个值
                            //     para.parent = {"id" : id}
                            // }
                            //把左边id带上请求
                            para.types_id=this.typeId;
                            this.$http.put("/Dictionary",para)
                                .then(result=>{
                                    this.editLoading = false;//关闭忙等框
                                    if(result.data.success){
                                        this.$message({
                                            message: '提交成功',
                                            type: 'success'
                                        });
                                    }else{
                                        this.$message({
                                            message: result.data.msg,
                                            type: 'error'
                                        });
                                    }
                                    this.dicFormVisible = false;
                                    this.getDictionarys();
                                }).catch(result=>{
                                this.$message({
                                    message: "访问数人数过多请稍后再试",
                                    type: 'error'
                                });
                            })

                        });
                    }
                    this.getDictionary()
                });
            },
            //右边列表获取
            detailList:function () {
                let typeId = this.typeId;
                if (typeId!=null){
                    //这里发送请求获取对应typeId的详细字典
                    this.$http.get("/Dictionary/detailList",{params:{"typeId":typeId}})
                        .then((res)=>{
                            //成功接收到数据
                            //diction
                            this.diction=res.data
                        })
                }
            },
            //左边表格点击触发事件
            selsChange: function (sels) {
                console.log(sels)
                this.typeId=sels.id;
                this.detailList();
            },
        },
        mounted() {
            //左边数据加载获取
            this.getDictionarys();
            //右边数据加载获取
            // this.getDictionary()
        },
    }

</script>

<style scoped>

</style>